import {
  getCarList,
  getTotalPay,
  getDeleteItem,
  getConfirm,
  getSubmit,
  getPay,
} from "../../services/index.js";
const state = {
  CarList: [],
  dxid: 0, //单选框下标
  carId: [], //选中商品的id
  settlement: [], //提交的信息
  actualTotal: 0, //提交的总价
  totalCount: 0, //提交的总数量
  userAddr: {},
  order: "",
  ress: {},
};

const mutations = {
  update(state, payload) {
    for (let key in payload) {
      state[key] = payload[key];
    }
  },

  //选中商品的id
  CarId(state, payload) {
    console.log("sdadf");
    state.CarList.forEach((item) => {
      if (item.flag) {
        state.carId.push(item.basketId);
      }
    });
    console.log(state.carId, "carid,,,");
  },
};
const actions = {
  //购物车数据
  async getCarList({ commit }, payload = {}) {
    let result = await getCarList(payload);
    if (result) {
      result[0].shopCartItemDiscounts[0].shopCartItems.forEach((item) => {
        item.flag = false;
      });
      commit("update", {
        CarList: result[0].shopCartItemDiscounts[0].shopCartItems,
      });
    }
    console.log(state.CarList, "sssss...");
  },

  //单选
  async getTotalPay({ commit }, payload = []) {
    let result = await getTotalPay(payload);
    if (result) {
      commit("CarId");
      console.log("单选...", result);
    }
  },

  //加减
  // async getChangeItem(
  //   { commit },
  //   payload = { count: 1, prodId: 71, shopId: 1, skuId: 385 }
  // ) {
  //   console.log(payload);
  //   let result = await getChangeItem(payload);
  //   if (result) {
  //     if (result.response.data === "库存不足") {
  //       state.CarList[].prodCount = 1;
  //     }
  //   }
  //   console.log("加减...", result);
  // },

  //删除
  async getDeleteItem({ commit }, arr) {
    console.log(arr, "del........");
    let result = await getDeleteItem(arr);
  },

  //结算
  async getConfirm(
    { commit },
    payload = {
      addrId: 0,
      basketIds: [162],
      couponIds: [],
      userChangeCoupon: 1,
    }
  ) {
    console.log(payload);
    let result = await getConfirm(payload);
    if (result) {
      commit("update", {
        settlement:
          result.shopCartOrders[0].shopCartItemDiscounts[0].shopCartItems,
        actualTotal: result.actualTotal,
        totalCount: result.totalCount,
        userAddr: result.userAddr,
      });
    }
    state.carId = [];
    console.log(state.carId);
  },

  //提交订单
  async getSubmit(
    { commit },
    payload = { orderShopParam: [{ remarks: "", shopId: 1 }] }
  ) {
    console.log(payload);
    let result = await getSubmit(payload);
    if (result) {
      let obj = { payType: 1, orderNumbers: result.orderNumbers };
      let res = await getPay(obj);
      commit("update", {
        ress: res,
      });
    }
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
